package com.mf.morefun.util;

import android.content.Context;
import android.util.Log;

import com.alibaba.sdk.android.oss.ClientConfiguration;
import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.OSS;
import com.alibaba.sdk.android.oss.OSSClient;
import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback;
import com.alibaba.sdk.android.oss.callback.OSSProgressCallback;
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSStsTokenCredentialProvider;
import com.alibaba.sdk.android.oss.internal.OSSAsyncTask;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.alibaba.sdk.android.oss.model.PutObjectResult;
import com.mf.morefun.bean.EventMessageBean;

import org.greenrobot.eventbus.EventBus;

//阿里云上传文件
public class UploadUtil{
    private long totalTime = 0;
    public void uploadFile(Context mContext, String accessKeyId, String accessKeySecret, String securityToken,String strEndPoint,String strBucketName, String fileName, String filePath, boolean isComment){
        // 在移动端建议使用STS的方式初始化OSSClient。
        OSSCredentialProvider credentialProvider = new OSSStsTokenCredentialProvider(accessKeyId, accessKeySecret, securityToken);
        ClientConfiguration conf = new ClientConfiguration();
        conf.setConnectionTimeout(15 * 1000); // 连接超时，默认15秒
        conf.setSocketTimeout(15 * 1000); // socket超时，默认15秒
        conf.setMaxConcurrentRequest(5); // 最大并发请求书，默认5个
        conf.setMaxErrorRetry(2); // 失败后最大重试次数，默认2次

      //  String strEndPoint ="oss-eu-central-1.aliyuncs.com";

//        String strEndPoint ="oss-cn-shanghai.aliyuncs.com";

        OSS oss = new OSSClient(mContext, strEndPoint, credentialProvider, conf);
        // 构造上传请求。
//        String strBucketName ="morefunreadingproduct";

     //   String strBucketName ="onlyfordaisy";

        String strKey = String.valueOf(System.nanoTime()) + "-" + fileName;

        PutObjectRequest put = new PutObjectRequest(strBucketName, strKey, filePath);

        // 异步上传时可以设置进度回调。
        put.setProgressCallback(new OSSProgressCallback<PutObjectRequest>() {
            @Override
            public void onProgress(PutObjectRequest request, long currentSize, long totalSize) {
                Log.d("PutObject", "currentSize: " + currentSize + " totalSize: " + totalSize);
                totalTime = totalSize;
            }
        });

        OSSAsyncTask task = oss.asyncPutObject(put, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
            @Override
            public void onSuccess(PutObjectRequest request, PutObjectResult result) {
                Log.d("PutObject", "UploadSuccess");
                Log.d("ETag", result.getETag());
                Log.d("RequestId", result.getRequestId());
                EventMessageBean messageBean = new EventMessageBean();
                if (isComment){
                    messageBean.setType(101111);
                }else {
                    messageBean.setType(101);
                }
                messageBean.setFileName(strKey);
                messageBean.setTime(totalTime);
                EventBus.getDefault().post(messageBean);
            }

            @Override
            public void onFailure(PutObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
                // 请求异常。
                if (clientExcepion != null) {
                    // 本地异常，如网络异常等。
                    clientExcepion.printStackTrace();
                }
                if (serviceException != null) {
                    // 服务异常。
                    Log.e("ErrorCode", serviceException.getErrorCode());
                    Log.e("RequestId", serviceException.getRequestId());
                    Log.e("HostId", serviceException.getHostId());
                    Log.e("RawMessage", serviceException.getRawMessage());
                }
                EventMessageBean messageBean = new EventMessageBean();
                messageBean.setType(102);
                messageBean.setFileName(serviceException.getRawMessage());
                EventBus.getDefault().post(messageBean);
            }
        });
    }
}
